/*
 * $Id: RuntimeConfigurationController.java 46 2013-05-03 21:38:12Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 46 $
 * $Date: 2013-05-03 21:38:12 +0000 (Fri, 03 May 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.web.controller.admin;

import com.sci.logisticsmap.service.LocationImportTemplateService;
import com.sci.logisticsmap.service.RuntimeConfigurationService;
import com.sci.logisticsmap.service.model.LocationImportTemplateLightDTModel;
import com.sci.logisticsmap.service.model.RuntimeConfigurationDTCommand;
import com.sci.logisticsmap.web.support.WebUtilities;
import com.sci.logisticsmap.web.model.RuntimeConfigurationVWCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * The Controller for processing runtime configuration admin view/ajax requests.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 46 $
 */
@Controller("admin.RuntimeConfigurationController")
@RequestMapping("")
public class RuntimeConfigurationController extends BaseController {
    /**
     * The runtime configuration admin path.
     */
    public static final String PATH_ADMIN_RUNTIME_CONFIGURATION = "/admin/runtime-configuration";

    /**
     * The runtime configuration admin view.
     */
    private final String viewRuntimeConfiguration = "admin/runtimeConfigurationPage";

    /**
     * The runtime configuration service.
     */
    @Autowired
    private RuntimeConfigurationService runtimeConfigurationService;
    /**
     * The Location import template service.
     */
    @Autowired
    private LocationImportTemplateService locationImportTemplateService;

    /**
     * The default constructor.
     * Initializes a Controller class.
     */
    public RuntimeConfigurationController() {
    }

    /*
     * (non-Javadoc)
     *
     * @see BaseController#init()
     */
    @PostConstruct
    public void init() {
        super.init();
    }

    /**
     * The method to response on the runtime configuration admin view request.
     *
     * @param  model
     *         The model.
     *
     * @return the runtime configuration admin view.
     */
    @RequestMapping(value=PATH_ADMIN_RUNTIME_CONFIGURATION, method={RequestMethod.GET})
    public String onRuntimeConfiguration(Model model) {
        try {
            RuntimeConfigurationDTCommand runtimeConfigurationCommand = runtimeConfigurationService.getCommand();
            model.addAttribute("runtimeConfigurationCommand", conversionUtilities.convert(runtimeConfigurationCommand, RuntimeConfigurationVWCommand.class));

            addLocationImportTemplateListToModel(model);
            addAllowedCountryCodeSelectOptionsToModel(model, runtimeConfigurationCommand.getAllowedCountryCodes());
        } catch (Exception e) {
            addExceptionToModel(model, e);
        }

        return viewRuntimeConfiguration;
    }

    /**
     * The method to response on the runtime configuration admin view submitting request.
     *
     * @param  command
     *         The command.
     * @param  result
     *         The validation binding result.
     * @param  model
     *         The model.
     *
     * @return the runtime configuration admin view.
     */
    @RequestMapping(value=PATH_ADMIN_RUNTIME_CONFIGURATION, method={RequestMethod.POST})
    public String onRuntimeConfigurationSubmit(@ModelAttribute("runtimeConfigurationCommand") RuntimeConfigurationVWCommand command, BindingResult result, Model model) {
        try {
            boolean failed = false;
            if (!result.hasErrors()) {
                try {
                    runtimeConfigurationService.edit(conversionUtilities.convert(command, RuntimeConfigurationDTCommand.class));
                    return buildRedirectToPath(PATH_ADMIN_RUNTIME_CONFIGURATION);
                } catch (Exception e) {
                    addExceptionToModel(model, e);
                    failed = true;
                }
            } else {
                failed = true;
            }

            if (failed) {
                addLocationImportTemplateListToModel(model);
                addAllowedCountryCodeSelectOptionsToModel(model, command.getAllowedCountryCodes());
            }
        } catch (Exception e) {
            addExceptionToModel(model, e);
        }

        return viewRuntimeConfiguration;
    }

    private void addLocationImportTemplateListToModel(Model model) {
        List<LocationImportTemplateLightDTModel> locationImportTemplateList = locationImportTemplateService.getLightDTModels();
        model.addAttribute("locationImportTemplateList", locationImportTemplateList);
    }

    private void addAllowedCountryCodeSelectOptionsToModel(Model model, String[] selectedCountryCodes) {
        model.addAttribute("allowedCountryCodeSelectOptions", WebUtilities.getCountryCodeSelectOptions(selectedCountryCodes));
    }
}
